#!/usr/bin/env python
# coding=utf-8

# check 股票基本面数据是否有没有保存在数据库中
# 因为现有的基本面数据是通过网络的api抓取下来的, 难免会出现timeout的情况, 导致一天的数据没有抓下来, 这个脚本是用来check这个用的
from pymongo import MongoClient
import tushare as ts
import numpy as np
import pandas as pd
import datetime
import json

def get_db():
    client = MongoClient("localhost", 27017)
    db = client.stock
    return db

if __name__ == "__main__":
    db = get_db()
    print(db)

    day = pd.Timestamp("2016-08-09")
    today = pd.Timestamp(str(datetime.date.today()))

    while day <= today:
        print("check day " + day.strftime('%Y-%m-%d'))
        t = db.stocks.find_one({'code': 'hs300', 'date':day.strftime('%Y-%m-%d')})
        if t != None:
            p = db.stocks_basics.find_one({'date':day.strftime('%Y-%m-%d')})
            if p == None:
                print("handle day " + day.strftime('%Y-%m-%d'))
                try:
                    data = ts.get_stock_basics(date=day.strftime('%Y-%m-%d'))
                except:
                    print("404 " + day.strftime('%Y-%m-%d'))
                else:
                    if not data.empty:
                        data['date'] = day.strftime('%Y-%m-%d')
                        data['code'] = data.index
                        db.stocks_basics.insert(json.loads(data.to_json(orient='records', date_format='iso')))
                        print(day.strftime('%Y-%m-%d') + " ok")

        day = day + datetime.timedelta(days=1)